﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ReadXml
{
    class GetEigenMatrix
    {
        public List<List<List<double>>> getEigenMatrix(List<List<List<double>>> array, int[] tarray, int lambda)
        {
            List<List<List<double>>> trainEigen = new List<List<List<double>>>();
            
            
            /*****  Generate random array  *****/


            for (int n = 0; n < array.Count; n++) //遍历训练集，得到每一个训练对象的，计算出了欧式距离的特征矩阵。
            {
                var kinectarray = array[n];

                int gLength = kinectarray[0].Count;

                
                //Console.WriteLine(kinectarray[1][1]);

                /***** Get characteristic points *****/
                List<List<double>> eigenMatrix = new List<List<double>>(); //定义三维list，作为特征矩阵模型           
                for (int i = 0; i < gLength; i++)
                {
                    if (i < 4)
                    {
                        List<double> list = new List<double>();
                        double distance = 0;
                        for (int j = 0; j < 3; j++)
                        {

                            distance += Math.Pow(kinectarray[j][i], 2);
                            //list.Add(kinectarray[j][0]);
                        }
                        distance = Math.Sqrt(distance);
                        //distance = 0;
                        list.Add(distance);
                        eigenMatrix.Add(list);
                    } 
                    else if (i < lambda && i != 0)
                    {
                        List<double> list = new List<double>();
                        for (int k = 0; k < i; k++)
                        {
                            double distance = 0, subtract;
                            for (int j = 0; j < 3; j++)
                            {
                                subtract = kinectarray[j][i] - kinectarray[j][k];
                                distance += Math.Pow(subtract, 2);
                                //Console.WriteLine(list[j]);
                            }
                            distance = Math.Sqrt(distance);
                            list.Add(distance);
                        }
                        eigenMatrix.Add(list);
                    }
                    else if (i >= lambda)
                    {
                        List<double> list = new List<double>();

                        for (int k = 0; k <lambda; k++)
                        {
                            double distance = 0, subtract;
                            for (int j = 0; j < 3; j++)
                            {
                                //subtract = kinectarray[j][i] - kinectarray[j][k]; //有问题，减去的应该是随机抽出来的序列。
                                //if (tarray[k] < kinectarray[j].Count)
                                //{
                                    subtract = kinectarray[j][i] - kinectarray[j][i-k-1]; //选取的是离该轨迹点最近的20个点
                                //subtract = kinectarray[j][i] - kinectarray[j][tarray[k]]; //加入了随机数组
                                //subtract = kinectarray[j][i] - kinectarray[j][k]; //这个是以序号为0到20个点为特征点
                                    distance += Math.Pow(subtract, 2);
                               // }
                                //横是j，竖是k，画图很好分辨
                            }
                            distance = Math.Sqrt(distance);
                            list.Add(distance);
                        }
                        eigenMatrix.Add(list);
                    }

                }

                trainEigen.Add(eigenMatrix);

                /*****  Print eigenMatrix  *****/
                /*
                for (int i = 0; i < eigenMatrix.Count; i++)
                {
                    for (int j = 0; j < eigenMatrix[i].Count; j++)
                    {
                        Console.Write(" {0:F4}", eigenMatrix[i][j]);
                    }
                    Console.WriteLine();
                    Console.WriteLine();
                    Console.WriteLine(eigenMatrix[i].Count);
                }*/
            }
            return trainEigen;
        }
    }
}
